package com.xiaocikuai.smallchunks.mapper.User;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaocikuai.smallchunks.entity.User.*;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Service;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;


@Mapper
public interface UserMapper extends BaseMapper<User> {


    @Select("SELECT nickname FROM user  ")
    List<String> getNickname();

    @Select("SELECT code FROM yshop_redemption_code LIMIT #{num}  ")
    List<String> getCode(int num);
    //用户查重
    @Select("SELECT COUNT(*) FROM user WHERE username = #{username} ")
    boolean chaxu (String username);

    @Select("SELECT uid FROM user WHERE uniquecode = #{uniquecode}")
    Integer selectSpreadId(String uniquecode);

    //登陆接口
    @Select("SELECT COUNT(*) FROM user WHERE username = #{username} AND password = #{password}")
    Boolean login(String username, String password);


    @Update("UPDATE user SET username = #{newUsername} WHERE username = #{username} AND password = #{password}")
    boolean update(String username,String newUsername, String password);


    //返回对应用户信息
    @Select("SELECT * FROM user WHERE username = #{username}")
    User userinfo(String username);



    // 根据查询条件统计符合条件的用户总数
    Long countByQuery(@Param("query") UserQuery query);

    // 根据查询条件进行查询并分页
    List<UserVO> searchAndPage(@Param("query") UserQuery query, int start, int pageSize);


    @Select("select count(*) from user")
    Long count();

    @Select("select *from user u left outer join userinfo ui on u.userinfo_id=ui.id  order by uid limit #{start},#{pagesize}")
    List<UserVO> page(Integer start,Integer pagesize);

    @Select("SELECT * FROM user_sign WHERE uid = #{uid} ORDER BY create_time DESC LIMIT 1")
    Sign getSignDate(int uid);

    @Insert("INSERT INTO user_sign (uid, title, number, balance, create_time, level) VALUES (#{uid}, #{title}, #{number}, #{balance}, #{createTime}, #{level})")
    void addSign(int uid, String title, int number, int balance, LocalDate createTime, int level );


    @Select("SELECT integral FROM user WHERE uid = #{uid} ")
    int getIntegrale(int uid);

    @Select("SELECT integrals FROM user WHERE uid = #{uid} ")
    int getIntegrales(int uid);

    @Update("UPDATE user SET integral = #{NewIntegral}, integrals = #{NewIntegrals} WHERE uid = #{uid} ")
    void updateIntegral(int NewIntegral, int NewIntegrals, int uid);

    @Update("UPDATE user SET integrals = integrals + #{score}, integral = integral + #{score} WHERE uid = #{uid}")
    boolean addIntegrals(int uid, int score);

    @Select("SELECT u.*, i.* FROM user u LEFT OUTER JOIN userinfo i ON u.userinfo_id = i.id WHERE u.uid = #{uid}")
    UserVO getUser(int uid);

    @Select("SELECT COUNT(*) FROM user_sign WHERE uid = #{uid} AND create_time = #{time}")
    Integer getIsSign(int uid,LocalDate time);

    @Update("UPDATE user SET update_time = #{time} WHERE username = #{username} ")
    boolean updateTime(String username, Date time);

    @Update("update user SET nickname = #{nickName} where uid= #{uid}")
    boolean updateNick(String nickName, int uid);


    @Update("update user SET password = #{code} where uid= #{uid}")
    boolean updateCode(String code ,int uid);


    @Update("update user SET avatar = #{img} where uid= #{uid}")
    boolean updateImg(String img ,int uid);

    @Select("Select balance FROM member_sale WHERE uid = #{uid} ORDER BY times DESC LIMIT 1")
    Integer getSaleBalance(int uid);


    @Insert("INSERT INTO member_sale (uid, money, divide, balance,status,open_id) VALUES (#{uid}, #{money}, #{divide}, #{balance}, #{status}, #{openId})")
    boolean insertMemberSale(int uid, int money,int divide,int balance,int status, String openId);

    @Select("select * from member_sale where uid= #{uid} order by times desc")
    List<MemberSale> selectMaleSale(int uid);

    @Update("UPDATE menber_sale SET divide = money WHERE id = #{id} AND uid = #{uid}")
    boolean updateMemberSale(MemberSale memberSale);
//    @Select("SELECT level FROM user WHERE uid = #{uid} ")


    @Select("select * from member_sale where money < 0 and divide <0 and status = -1")
    List<MemberSale> selectMale();

    @Select("Select sum(divide) FROM member_sale WHERE money > 0 and uid= #{uid} AND DATE(times) = CURDATE() - INTERVAL 1 DAY ")
    Integer getYesterdayPayouts(int uid);

    @Select("Select sum(divide) FROM member_sale WHERE uid= #{uid} AND DATE(times) = CURDATE() ")
    Integer getTodayPayouts(int uid);

}
